package com.example.controller;

import com.example.entity.CapacityInformationAirspace;
import com.example.entity.condition.AlarmInformationCondition;
import com.example.entity.request.SearchWholeDayDataRequest;
import com.example.service.CapacityInformationService;
import com.example.util.MicroResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/alarm-information")
public class CapacityInformationController {

    @Autowired
    private CapacityInformationService service;

    // 查找所有告警信息
    @GetMapping("/find")
    public ResponseEntity findAll() {
        return ResponseEntity.ok(MicroResponseUtils.success(service.findAll()));
    }

    // 分页查询告警信息
    @PostMapping("/page")
    public ResponseEntity page(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody AlarmInformationCondition condition) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.page(pageNum, pageSize, condition)));
    }

    // 分析所有符合条件的告警信息（饼状图）
    @PostMapping("/analysis")
    public ResponseEntity analysis(@RequestBody AlarmInformationCondition condition) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.analysis(condition)));
    }

    // 分析所有符合条件的告警信息（条形图）
    @PostMapping("/analysisDuplicate")
    public ResponseEntity analysisDuplicate(@RequestBody AlarmInformationCondition condition) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.analysis(condition)));
    }

    // 定时触发模拟数据生成任务，每 15 分钟执行一次
    @Scheduled(cron = "0 0,15,30,45 * * * ?")
    @GetMapping("/trigger")
    public void executeTask() {
        System.out.println("Task executed at: " + java.time.LocalDateTime.now());
        service.triggerMock();
    }

    // 根据请求查询全天告警数据
    @PostMapping("/searchwholedata")
    public ResponseEntity searchWholeData(@RequestBody SearchWholeDayDataRequest request) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.searchWholeData(request)));
    }

    // 按 ID 查找告警信息 (新增功能)
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable Integer id) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.findById(id)));
    }

    // 保存告警信息 (新增功能)
    @PostMapping("/save")
    public ResponseEntity save(@RequestBody CapacityInformationAirspace alarm) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.save(alarm)));
    }

    // 更新告警信息 (新增功能)
    @PutMapping("/update")
    public ResponseEntity update(@RequestBody CapacityInformationAirspace alarm) {
        return ResponseEntity.ok(MicroResponseUtils.success(service.update(alarm)));
    }

    @PostMapping("/selectall")
    public ResponseEntity selectAll(@RequestBody AlarmInformationCondition condition){
        return ResponseEntity.ok(MicroResponseUtils.success(service.selectAll(condition)));
    }
}
